package com.zzgele.qmzb.api.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zzgele.qmzb.api.core.AbstractService;
import com.zzgele.qmzb.api.core.Constants;
import com.zzgele.qmzb.api.model.BonusPackage;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.service.BonusPackageService;
import com.zzgele.qmzb.api.service.UserService;
import com.zzgele.qmzb.api.utils.HongBaoAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;


/**
 * Created by Martin on 2017/07/14.
 */
@Service(interfaceClass = BonusPackageService.class,proxy="jdk")
@Component
@Transactional
public class BonusPackageServiceImpl extends AbstractService<BonusPackage> implements BonusPackageService {
    
    private  final Logger logger = LoggerFactory.getLogger(BonusPackageServiceImpl.class);
    @Resource
    private UserService userService;
//    @Resource
//    private BonusPackageMapper bonusPackageMapper;
     @Resource
     private BonusPackageService bonusPackageService;
    /**
     * @param id
     * @return 查询无结果返回null
     */
    public BonusPackage getBonusPackageById(Long id) {
        BonusPackage bonusPackage =bonusPackageService.findById(id);// bonusPackageMapper.selectBonusPackageById(id);
        if (bonusPackage == null) {
            return null;
        }
        return bonusPackage;
    }
    /**
     * 创建数量为amount个的红包，每个红包的金额为随机，最大金额为maxPrice 2
     *
     * @param amount
     * @param expireTime 过期时间,这个过期时间指红包最晚可领取时间，不是消费时间
     * @return
     */
    public BonusPackage createTotalPriceBonusPackage(Integer amount, Integer price, Integer expireTime, Long uid){
        String prePrice = HongBaoAlgorithm.splitRedPackets(price, amount);
        BonusPackage bonusPackage = new BonusPackage();
        bonusPackage.setUid(uid);
        bonusPackage.setAmount(amount);
        bonusPackage.setPrePrice(prePrice);
        bonusPackage.setExpireTime(expireTime);
        bonusPackage.setPrice(price);
        bonusPackage.setStatus(0);
        //扣手续费
        Double my_price=price* Constants.COMMISSION_BONUS;
        Integer temp=my_price.intValue();
        Integer change=price-temp;
        //存日志

        //扣钻石
        User user=userService.findById(uid);
        if(user.getDiamond().intValue()<price){
            return  null;
        }
        HashMap map = new HashMap();
        map.put("change", price);
        map.put("uid", uid);
        userService.setDec(map);
        bonusPackageService.save(bonusPackage);
        return bonusPackage;
    }
    /**
     * 传入总额，传入数量,返回数量的随机金额
     *
     * @param moneySum
     * @param redNum
     * @return
     */
//    private  String wxAlgorithm(Integer moneySum, int redNum) {
//        StringBuffer stringBuffer = new StringBuffer();
//        // 设置最小的金额
//        Integer moneyMin = 1;
//        Random random = new Random();
//        //精确小数点2位
////        NumberFormat formatter = new DecimalFormat("#.##");
//        for (int i = 1; i < redNum; i++) {
//            //随机一个数，数值范围在最小值与余额之间
//            Integer money =(random.nextInt() * (moneySum - moneyMin) + moneyMin);
//            //数值转换
//            moneySum = moneySum - Integer.valueOf(money);
//
//            logger.info("第" + i + "个红包：" + money + "元 ,余额：" + moneySum);
//            if (stringBuffer.length() == 0) {
//                stringBuffer.append(money);
//            } else {
//                stringBuffer.append("," + money);
//            }
//        }
//
//        logger.info("最后个红包：" + moneySum + "元 ,余额：" + (moneySum - moneySum));
//        return stringBuffer.toString();
//    }




}
